package com.example.music08.utils;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.bumptech.glide.load.resource.bitmap.BitmapEncoder;
import com.example.music08.entity.Music;
import com.example.music08.entity.Pinglun;

import java.util.ArrayList;
import java.util.List;

//数据库工具类
public class SqlUtils {
    private MySqlite mySqlite;
    private SQLiteDatabase db;

    private SqlUtils(Context context){
        mySqlite = new MySqlite(context);
        db = mySqlite.getWritableDatabase();
    }
    private static SqlUtils sqlUtils;
    public static SqlUtils getInstance(Context context){
        if(sqlUtils==null){
            sqlUtils = new SqlUtils(context);
        }
        return sqlUtils;
    }
    //新增数据---收藏  播放过的   点赞    table:表名，这样就可以只封装一套增删改查了
    public void insert(Music.DataBean bean,String table){
        ContentValues values = new ContentValues();
        values.put("id",bean.getId());
        values.put("name", bean.getName());
        values.put("singer",bean.getSinger());
        values.put("data",bean.getData());
        values.put("lrc",bean.getLrc());
        values.put("duration",bean.getDuration());
        values.put("picUrl",bean.getPicUrl());
        db.insert(table,null,values);
    }

    /**
     * 查询所有---没有任何条件
     * 查询符合某种条件--例如歌曲名称包含  某个字段  --歌曲名称
     * 查询具体某个值---id
     */
    @SuppressLint("Range")
    public List<Music.DataBean> query(String table, String key, String value){
        List<Music.DataBean> list = new ArrayList<>();//用来接收符合条件的数据
        Cursor cursor;
        if(!key.isEmpty()&&!value.isEmpty()){//有条件的查询
            cursor = db.query(table,null,key+"=?",new String[]{value},null,null,"duration desc");//按时长倒序排序
        }else {//查询所有
            cursor = db.query(table,null,null,null,null,null,null);
        }
        if(cursor!=null){
            while (cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                int duration = cursor.getInt(cursor.getColumnIndex("duration"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                String singer = cursor.getString(cursor.getColumnIndex("singer"));
                String data = cursor.getString(cursor.getColumnIndex("data"));
                String lrc = cursor.getString(cursor.getColumnIndex("lrc"));
                String picUrl = cursor.getString(cursor.getColumnIndex("picUrl"));

                list.add(new Music.DataBean(id,name,singer,data,picUrl,duration,lrc));
            }
            cursor.close();//循环外边关闭游标
        }
        return list;
    }
    //删除指定值---一般根据id删除
    public void delete(String table,String id){
        db.delete(table,"id=?",new String[]{id});
    }
    //评论表的操作--新增   查询
    public void insertPl(Pinglun pl){
        ContentValues values = new ContentValues();
        values.put("id",pl.getId());
        values.put("username",pl.getUsername());
        values.put("msg",pl.getMsg());
        db.insert("pl",null,values);
    }
    @SuppressLint("Range")
    public List<Pinglun> query(){
        List<Pinglun> list = new ArrayList<>();
        Cursor cursor = db.query("pl", null, null, null, null, null, null);
        if(cursor!=null){
            while (cursor.moveToNext()){
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String username = cursor.getString(cursor.getColumnIndex("username"));
                String msg = cursor.getString(cursor.getColumnIndex("msg"));
                list.add(new Pinglun(id,username,msg));
            }
            cursor.close();
        }
        return list;
    }

}
